package com.medical.data.controller;

import com.medical.data.domain.FormData;
import com.medical.data.domain.FormTemplate;
import com.medical.data.service.FormTemplateService;
import com.medical.data.utils.Result;
import com.medical.data.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/form")
@Api(description = "表单管理")
@Slf4j
public class FormController {
    @Autowired
    FormTemplateService formTemplateService;

    @ApiOperation("创建表单模版，从项目里面创建,里面有节点")
    @PostMapping("/createFormByNode")
    public Result creatFormByNode(@Validated @RequestBody FormNodeVo formNodeVo) {
        log.info("/front/form/createFormByNode参数========>{}", formNodeVo);
        FormTemplate formTemplate = formTemplateService.creatFormByNode(formNodeVo);
        return Result.ok().data("form", formTemplate);
    }

    @ApiOperation("创建表单模版,从表单模块里面创建")
    @PostMapping("/createForm")
    public Result createForm(@Validated @RequestBody FormByFormTamplate formByFormTamplate) {
        log.info("/front/form/createForm参数========>{}", formByFormTamplate);
        FormTemplate formTemplate = formTemplateService.createForm(formByFormTamplate);
        return Result.ok().data("form", formTemplate);
    }

    @ApiOperation("获取全部表单,导入表单模块中的接口")
    @PostMapping("/getAllForm")
    public Result getAllForm(@Validated @RequestBody AllFormVo allFormVo) {
        log.info("/front/form/getAllForm参数========>{}", allFormVo);
        List<FormResultVo> formResultVos = formTemplateService.getAllForm(allFormVo);
        return Result.ok().data("formResultVos", formResultVos);
    }

    @ApiOperation("导入表单")
    @PostMapping("/importForm")
    public Result importForm(@Validated @RequestBody ImportFormVo importFormVo) {
        log.info("/front/form/importForm参数========>{}", importFormVo);
        formTemplateService.importForm(importFormVo);
        return Result.ok();
    }

    @ApiOperation("删除表单")
    @PostMapping("/deleteForm/{formId}")
    public Result deleteForm(@NotBlank(message = "表单id不可为空") @PathVariable String formId) {
        log.info("/front/form/deleteForm参数========>{}", formId);
        formTemplateService.deleteForm(formId);
        return Result.ok();
    }

    @ApiOperation("修改表单")
    @PostMapping("/updateForm")
    public Result updateForm(@Validated @RequestBody UpdateFormVo updateFormVo) {
        log.info("/front/form/updateForm参数========>{}", updateFormVo);
        formTemplateService.updateForm(updateFormVo);
        return Result.ok();
    }

    @ApiOperation("根据节点查询表单")
    @PostMapping("/getFormByNode/{projectId}/{nodeId}")
    public Result getFormByNode(@PathVariable String projectId, @PathVariable String nodeId) {
        log.info("/front/form/getFormByNode参数========>{}", nodeId, projectId);
        List<FormResultVo> formResultVos = formTemplateService.getFormByNode(nodeId, projectId);
        return Result.ok().data("formResultVos", formResultVos);
    }

    @ApiOperation("查询该用户下的所有的表单")
    @PostMapping("/getFormByUser")
    public Result getFormByUser(@RequestBody(required = false) QueryFormVo queryFormVo) {
        log.info("/front/form/getFormByUser参数========>{}", queryFormVo);
        Map<String, Object> formResultVos = formTemplateService.getFormByUser(queryFormVo);
        return Result.ok().data(formResultVos);
    }

    @ApiOperation("获取表单详情信息")
    @PostMapping("/getFormDetails/{formId}")
    public Result getFormDetails(@NotBlank(message = "表单id不可为空") @PathVariable String formId) {
        log.info("/front/form/getFormDetails参数========>{}", formId);
        FormTemplate formTemplate = formTemplateService.getById(formId);
        return Result.ok().data("form", formTemplate);
    }

    @ApiOperation("保存至表单中")
    @PostMapping("/savaForm")
    public Result savaForm(@Validated @RequestBody SavaFormVo savaFormVo) {
        log.info("/front/form/savaForm参数========>{}", savaFormVo);
        formTemplateService.savaForm(savaFormVo);
        return Result.ok();
    }

    @ApiOperation("删除在节点中的表单")
    @PostMapping("/deleteFormInNode/{formId}")
    public Result deleteFormInNode(@PathVariable String formId) {
        log.info("/front/form/deleteFormInNode参数========>{}", formId);
        formTemplateService.deleteFormInNode(formId);
        return Result.ok();
    }

    @ApiOperation("删除表单广场中的表单")
    @PostMapping("/deleteFormInModel/{formId}")
    public Result deleteFormInModel(@PathVariable String formId) {
        log.info("/front/form/deleteFormInModel参数========>{}", formId);
        formTemplateService.deleteFormInModel(formId);
        return Result.ok();
    }

    @ApiOperation("修改表单信息")
    @PostMapping("/changeFormMessage/{formId}/{formName}")
    public Result changeFormMessage(@PathVariable String formId, @PathVariable String formName) {
        log.info("/front/form/changeFormMessage参数========>{}", formId, formName);
        formTemplateService.changeFormMessage(formId, formName);
        return Result.ok();
    }

    @ApiOperation("共享表单模版到模版广场")
    @PostMapping("/share/{formId}")
    public Result share(@PathVariable String formId) {
        log.info("/front/form/changeFormMessage参数========>{}", formId);
        formTemplateService.shareFormTemplate(formId);
        return Result.ok();
    }

}

